package com.acenodie.acenodieopenglturorial.Camera1Demo2;

import android.Manifest;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.acenodie.acenodieopenglturorial.Camera1Demo.Camera1Activity;
import com.acenodie.acenodieopenglturorial.Camera1Demo.CameraView;
import com.acenodie.acenodieopenglturorial.R;
import com.acenodie.acenodieopenglturorial.utils.PermissionUtils;

//本Demo采用:
//SurfaceView + EGL +SurfaceTexture
public class Camera1Activity2 extends AppCompatActivity {

    private CameraView2 mCameraView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PermissionUtils.askPermission(this,new String[]{Manifest.permission.CAMERA,Manifest
                .permission.WRITE_EXTERNAL_STORAGE},10,initViewRunnable);
    }

    private Runnable initViewRunnable=new Runnable() {
        @Override
        public void run() {
            setContentView(R.layout.activity_camera12);
            mCameraView2= (CameraView2)findViewById(R.id.mCameraView);

        }
    };

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionUtils.onRequestPermissionsResult(requestCode == 10, grantResults, initViewRunnable,
                new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(Camera1Activity2.this, "没有获得必要的权限", Toast.LENGTH_SHORT).show();
                        finish();
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mCameraView2.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mCameraView2.onPause();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add("切换摄像头").setTitle("切换摄像头").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        String name=item.getTitle().toString();
        if(name.equals("切换摄像头")){
            mCameraView2.switchCamera();
        }
        return super.onOptionsItemSelected(item);
    }
}
